<template>
  <div class="table-box">
    <ProTable ref="proTableRef" title="字典列表" row-key="id" :columns="columns" :request-api="loadData" highlight-current-row>
      <!-- 表格 header 按钮 -->
      <template #tableHeader>
        <ElButton type="primary" :icon="CirclePlus" @click="onAddClicked">新增</ElButton>
      </template>
      <template #systemFlag="scope">
        <ElTag v-if="scope.row.systemFlag === '0'" type="success">业务类</ElTag>
        <ElTag v-else type="danger">系统类</ElTag>
      </template>
      <!-- 字典操作 -->
      <template #operation="scope">
        <ElButton type="primary" link :icon="View" @click="onViewClicked(scope.row)">查看</ElButton>
        <ElButton type="primary" link :icon="EditPen" @click="onEditClicked(scope.row)">编辑</ElButton>
        <ElButton type="primary" link :icon="Delete" @click="onDeleteClicked(scope.row)">删除</ElButton>
        <ElButton type="primary" link :icon="Present" @click="onItemClicked(scope.row)">字典项</ElButton>
      </template>
    </ProTable>
    <DictForm ref="formRef" @success="refresh" />
    <ItemList ref="itemListRef" />
  </div>
</template>

<script setup lang="ts" name="ZLDict">
import { CirclePlus, Delete, EditPen, Present, View } from '@element-plus/icons-vue'
import ProTable from '@/components/ProTable/index.vue'
import { ColumnProps, ProTableInstance } from '@/components/ProTable/interface'
import { ElMessage, ElMessageBox } from 'element-plus'
import DictForm from './dict-form.vue'
import { ref } from 'vue'
import ItemList from '@views/uc/dict/item-list.vue'
import { pageDict, removeDict } from '@api/uc/dict'
import { Dict } from '@api/uc/dict/interface'
import { FormType } from '@/enums'
import { IsSystemEnum } from './dict'
const proTableRef = ref<ProTableInstance>()
// 打开 drawer(新增、查看、编辑)
const formRef = ref<InstanceType<typeof DictForm> | null>(null)
const itemListRef = ref<InstanceType<typeof ItemList> | null>(null)

const columns: ColumnProps[] = [
  { type: 'index', label: '序号', width: 60, index: getIndex },
  { prop: 'type', label: '类型', search: { el: 'input', key: 'type' } },
  { prop: 'description', label: '描述' },
  {
    prop: 'isSystem',
    label: '字典类型',
    search: { el: 'select' },
    enum: IsSystemEnum,
    tag: true
  },
  { prop: 'remark', label: '备注信息' },
  { prop: 'operation', label: '操作' }
]

// 获取序号
function getIndex(index: number) {
  const { current, size } = proTableRef.value!.pageable
  return (current - 1) * size + index + 1
}

// 加载数据
function loadData(query: Dict.Query) {
  query.order = 'update_time desc'
  return pageDict(query)
}

// 刷新列表
function refresh() {
  proTableRef.value?.getTableList()
}

// 添加
function onAddClicked() {
  formRef.value?.open(FormType.ADD)
}

// 查看
const onViewClicked = (row: any) => {
  formRef.value?.open(FormType.VIEW, row.id)
}

// 编辑
function onEditClicked(row: any) {
  formRef.value?.open(FormType.EDIT, row.id)
}

// 删除
function onDeleteClicked(row: any) {
  ElMessageBox.confirm('您确定要删除' + row.description + '吗?', '温馨提示', {
    confirmButtonText: '确定',
    cancelButtonText: '取消',
    type: 'warning'
  })
    .then(() => {
      removeDict(row.id).then(() => {
        ElMessage({
          type: 'success',
          message: '删除成功!'
        })
        proTableRef.value?.getTableList()
      })
    })
    .catch(() => {
      ElMessage({
        type: 'warning',
        message: '删除取消!'
      })
    })
}

// 打开字典项表单
function onItemClicked(row: any) {
  itemListRef.value?.open(row.id)
}
</script>
